package com.example.controller;

import com.example.pojo.Employee;
import com.example.service.EmployeeServiceImpe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

//判断登录的控制器
@Controller
public class LoginController {

    @Autowired
    private EmployeeServiceImpe employeeServiceImpe;

    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String lastName, @RequestParam("password") String password, Model model, HttpSession session) {

        //调用service层的getEmployeeByName()方法，返回查询到的员工信息
        Employee employee = employeeServiceImpe.getEmployeeByName(lastName);
        //员工信息不为空并且密码正确
        if (employee != null && password.equals(employee.getPassword())) {
            session.setAttribute("loginUser", lastName);
            //为了用户名、密码不显露在地址栏中，重定向一个不存在的页面，再根据路由进行跳转
            return "redirect:/main.html";
        } else {
            model.addAttribute("msg", "用户名或密码错误");
            return "/index";
        }
    }
}
